//调用request.js中的request方法
import request from '@/utils/request.js'

//注册
export const userRegisterService = (registerData) => {
    //借助UrlSearchParams对象,将对象转换成查询字符串
    const params = new URLSearchParams()
    //遍历对象,将对象的属性和值添加到params中
    for (let key in registerData) {
        params.append(key, registerData[key])//将对象的属性和值添加到params中
    }
    return request.post('/user/register', params)
}

//登录
export const userLoginService = (loginData) => {
    //借助UrlSearchParams对象,将对象转换成查询字符串
    const params = new URLSearchParams()
    //遍历对象,将对象的属性和值添加到params中
    for (let key in loginData) {
        params.append(key, loginData[key])//将对象的属性和值添加到params中
    }
    return request.post('/user/login', params)
}

//获取用户列表
export const getUserListService = () => {
    return request.get('/user/all')
}

//获取用户信息
export const getUserInfoService = () => {
    return request.get('/user/info')
}

//获取用户帖子数量
export const getUserPostCountService = (userId) => {
    return request.get('/user/postCount', {params: {userId}})
}

//获取用户信息--根据用户id
export const getUserInfoByIdService = (userId) => {
    return request.get('/user/infoById', {params: {userId}})
}

//修改用户头像接口的函数
export const updateUserAvatarService = (avatar) => {
    return request.patch('/user/updateAvatar', avatar, {
        headers: {
            'Content-Type': 'text/plain'
        }
    })
}

//修改用户信息
export const updateUserInfoService = (userInfo) => {
    return request.put('/user/updateInfo', userInfo,)
}

//修改用户信息--管理员
export const adminUpdateUserInfoService = (userInfo) => {
    return request.put('/user/admin/updateInfo', userInfo,)
}
// 批量更新用户状态
export const batchUpdateUserStatusService = (userIds, status) => {
    return request.put('/user/batchUpdateStatus', {userIds, status})
}

// 批量更新用户角色
export const batchUpdateUserRoleService = (userIds, role) => {
    return request.put('/user/batchUpdateRole', {userIds, role})
}

// 批量更新用户认证状态
export const batchUpdateUserVerificationService = (userIds, isVerified) => {
    return request.put('/user/batchUpdateVerification', {userIds, isVerified})
}

// 添加用户
export const addUserService = (userData) => {
    const params = new URLSearchParams()
    for (let key in userData) {
        params.append(key, userData[key])
    }
    return request.post('/user/add', params)
}

// 删除用户
export const deleteUsersService = (userIds) => {
    return request.delete('/user/delete', {data: {userIds}})
}
